// JavaScript Document
function filterulice(forma){
	var els = forma.elements;
	var ul = trim(els["ulica_filter"].value);
	LoadRSDoc('geos.php?a=ulice&ul='+ul,'setContent');
}

function GoPage(page,last){
	if(page<1){
		page=1;
	}
	if(typeof(last) != "undefined"){
		last = parseInt(last);
		if(page>last){
			page=last
		}
	}
	var ul = document.getElementById("ulica_filter");
	var action = "geos.php?a=ulice&page="+page;
	if(trim(ul.value).length>0){
		action += "&ul="+trim(ul.value);
	}
	LoadRSDoc(action,'setContent');
}
function novaUlica(){
	var nu = prompt("Naziv ulice: ","");
	if(trim(nu).length>0){
		if(confirm("Potvrdite dodavanje nove ulice: "+nu)){
			LoadRSDoc("geos.php?a=ulice&mod=add&u="+nu,'afterNovaUlica');
		}
	}
}
function afterNovaUlica(response){
	alert(response);
	LoadRSDoc("geos.php?a=ulice",'setContent');
}
function editUlica(tr,id,name){
	var filter = trim(document.getElementById("ulica_filter").value);
	var newName = prompt("Naziv ulice:",name);
	if(newName.length && newName!=name){
		LoadRSDoc("geos.php?a=ulice&mod=save&id="+id+"&ul="+escape(newName),"afterEditUlica('"+filter+"')");
	}
}
function afterEditUlica(response, filter){
	alert(response);
	var action = "a=ulice";
	action += "&ul="+filter;
	LoadRSDoc("geos.php","setContent",action,"",1);
}
function filterAdminLokacije(btn){
	var p = trim(btn.innerHTML);
	var forma = document.getElementById("lokacijeAdminForm");
	if(forma){
		var page;
		if(typeof(forma.page)!="undefined"){
			page = forma.page
		}else{
			page = document.createElement("input");
			page.type = "hidden";
			page.name = page.id = "page";
			forma.appendChild(page);
		}
		page.value = p;
		forma.submit.click();
	}
}
function updateFilterLokacijeParent(sel,names){
	if(typeof(names) != "object"){
		names = Array("filterParentGrad","filterParentLokOpstina","filterParentNaselje");
	}
	var pg  = document.getElementById(names[0]);
	var plo = document.getElementById(names[1]);
	var pn  = document.getElementById(names[2]);
	switch(sel.value){
		case "1":
			pg.style.display = "block";
			plo.style.display = "none";
			pn.style.display = "none";
			break;
		case "2":
			pg.style.display = "none";
			plo.style.display = "block";
			pn.style.display = "none";
			break;
		case "3":
			pg.style.display = "none";
			plo.style.display = "none";
			pn.style.display = "block";
			break;
		default:
			pg.style.display = "none";
			plo.style.display = "none";
			pn.style.display = "none";
			break;
	}
}
function editLokacija(lid,lok_ptt,lok_naziv,tipid,parentid){
	/*var newname = prompt("Novi naziv:",lok_naziv);
	newname = trim(newname);
	if((newname != lok_naziv) && newname.length>0){
		LoadRSDoc("geos.php?a=lokacije&mod=save&lid="+lid+"&naziv="+escape(newname),'afterSaveLok');
	}*/
	var tipsel = document.getElementById("filter_tip");
	var parGradSel = document.getElementById("filter_parent_grad");
	var parLokOpsSel = document.getElementById("filter_parent_lokopstina");
	var parNaseljeSel = document.getElementById("filter_parent_naselje");
	
	if(tipsel && parGradSel && parLokOpsSel && parNaseljeSel){
		var html = '<div class="area" id="editContainer">';
		html += '<form action="javascript:void(0)" onsubmit="submitForm(this,\'geos.php\',\'POST\',\'afterSaveLok\',1)">';
		html += '<input type="hidden" name="a" value="lokacije" />';
		html += '<input type="hidden" name="mod" value="save" />';
		html += '<input type="hidden" name="lid" value="'+lid+'" />';
		html += '<input type="hidden" name="oldType" value="'+tipid+'" />';
		html += '<input type="hidden" name="oldParent" value="'+parentid+'" />';
		
		html += '<h2>Lokacija #'+lid+'</h2>';
		html += '<label for="lok_ptt">PTT:</label><input type="text" name="lok_ptt" id="lok_ptt" value="'+lok_ptt+'">';
		html += '<label for="lok_name">Naziv:</label><input type="text" name="lok_name" id="lok_name" value="'+lok_naziv+'">';

		// TIP SELECT
		var opts = tipsel.options;
		html += '<label for="lok_tip">Tip:</label>';
		html += '<select name="lok_tip" id="lok_tip" onchange="updateFilterLokacijeParent(this,Array(\'parGradSpan\',\'parLokOpsSpan\',\'parNaseljeSpan\'))">';
		for(var i=0; i<opts.length; i++){
			if(opts[i].value>0){
				html += '<option value="'+opts[i].value+'"';
				if(opts[i].value == tipid){
					html += ' selected';
				}
				html += '>'+opts[i].innerHTML+'</option>';
			}
		}
		html += '</select>';
		// GRAD-PARENT SELECT
		var opts = parGradSel.options;
		html += '<span id="parGradSpan"';
		if(tipid != 1){
			html += ' style="display:none;"';
		}
		html += ">";
		html += '<label for="lok_grad_parent">Opština:</label>';
		html += '<select name="lok_grad_parent" id="lok_grad_parent"';
		html += '>';
		for(var i=0; i<opts.length; i++){
			if(opts[i].value>0){
				html += '<option title="'+opts[i].value+'" value="'+opts[i].value+'"';
				if(tipid==1 && opts[i].value == parentid){
					html += ' selected';
				}
				html += '>'+opts[i].innerHTML+'</option>';
			}
		}
		html += '</select></span>';
		// LOK.OPSTINA-PARENT SELECT
		var opts = parLokOpsSel.options;
		html += '<span id="parLokOpsSpan"';
		if(tipid != 2){
			html += ' style="display:none;"';
		}
		html += ">";
		html += '<label for="lok_lokops_parent">Mesto:</label>';
		html += '<select name="lok_lokops_parent" id="lok_lokops_parent">';
		for(var i=0; i<opts.length; i++){
			if(opts[i].value>0){
				html += '<option title="'+opts[i].value+'" value="'+opts[i].value+'"';
				if(tipid==2 && opts[i].value == parentid){
					html += ' style="color:#aaa" selected';
				}else if(tipid==1 && opts[i].value==lid){
					html += ' disabled';
				}
				html += '>'+opts[i].innerHTML+'</option>';
			}
		}
		html += '</select></span>';
		// NASELJE-PARENT SELECT
		var opts = parNaseljeSel.options;
		html += '<span id="parNaseljeSpan"';
		if(tipid != 3){
			html += ' style="display:none;"';
		}
		html += ">";
		html += '<label for="lok_naselje_parent">Lok.opština:</label>';
		html += '<select name="lok_naselje_parent" id="lok_naselje_parent">';
		for(var i=0; i<opts.length; i++){
			if(opts[i].value>0){
				html += '<option title="'+opts[i].value+'" value="'+opts[i].value+'"';
				if(tipid==3 && opts[i].value == parentid){
					html += ' selected';
				}else if(tipid==2 && opts[i].value==lid){
					html += ' disabled';
				}
				html += '>'+opts[i].innerHTML+'</option>';
			}
		}
		html += '</select></span>';
		
		html += '<br clear="all" /><input type="submit" value="sačuvaj" class="save" />';
		html += '</form>';
		html += '</div>';
		ypAjaxForm(html);
	}
	
}
function afterSaveLok(response){
	alert(response);
	closeYpAjaxForm();
	var forma = document.getElementById("lokacijeAdminForm");
	forma.submit.click();
}
function novaLokacija(){
	var d = document.getElementById("newLokTemplate");
	d.style.display=(d.style.display=="block")?"none":"block";
}
function afterNovaLokacija(response){
	alert(response)
}
function onchangeNewTip(sel){
	var tip = parseInt(sel.value);
	var pg = document.getElementById("n_parent_grad");
	var pl = document.getElementById("n_parent_lops");
	var pn = document.getElementById("n_parent_nas");
	var p = document.getElementById("new_parent");
	switch(tip){
		case 1:
			pg.style.display="block";
			pl.style.display="none";
			pn.style.display="none";
			p.value = pg.getElementsByTagName("select")[0].value;
			break;
		case 2:
			pg.style.display="none";
			pl.style.display="block";
			pn.style.display="none";
			p.value = pl.getElementsByTagName("select")[0].value;
			break;
		case 3:
			pg.style.display="none";
			pl.style.display="none";
			pn.style.display="block";
			p.value = pn.getElementsByTagName("select")[0].value;
			break;
		default:
			pg.style.display="none";
			pl.style.display="none";
			pn.style.display="none";
			p.value = "";
			break;
	}
}
function editRegion(regid, regname){
	var newname = prompt("novi naziv:",regname);
	if(newname.length && newname!=regname){
		//alert("Izmenjeno:\n"+regid+". "+newname);
		LoadRSDoc("geos.php?a=regioni&mod=save&id="+regid+"&naziv="+escape(newname),'afterSaveRegion');
	}
}
function afterSaveRegion(response){
	alert(response);
	var form = document.getElementById("adminRegioni");
	form.submit.click();
}
function editOpstina(opsid, opsname, regid, regname){
	var regsel = document.getElementById("filter_region");
	if(regsel){
		var opts = regsel.options;
		var html = '<div class="area" id="editContainer">';
		html += '<form action="javascript:void(0)" onsubmit="submitForm(this,\'geos.php\',\'POST\',\'afterSaveOpstina\',1)">';
		html += '<input type="hidden" name="a" value="opstine" />';
		html += '<input type="hidden" name="mod" value="save" />';
		html += '<input type="hidden" name="edit_opsid" value="'+opsid+'" />';
		
		html += '<h2>Opština #'+opsid+'</h2>';
		html += '<label for="edit_opsname">Naziv:</label><input type="text" name="edit_opsname" id="edit_opsname" value="'+opsname+'">';
		html += '<label for="edit_region">Region:</label>';
		html += '<select name="edit_region" id="edit_region">';
		for(var i=0; i<opts.length; i++){
			if(opts[i].value>0){
				html += '<option value="'+opts[i].value+'"';
				if(opts[i].value == regid){
					html += ' selected';
				}
				html += '>'+opts[i].innerHTML+'</option>';
			}
		}
		html += '</select><br clear="all" />';
		html += '<input type="submit" value="sačuvaj" class="save" />';
		html += '</form>';
		html += '</div>';
		ypAjaxForm(html);
	}
}
function afterSaveOpstina(resp){
	alert(resp);
	closeYpAjaxForm();
	var forma = document.getElementById("adminOpstine");
	if(forma){
		forma.submit.click();
	}
}
function filterAdminOpstine(btn){
	var p = trim(btn.innerHTML);
	var forma = document.getElementById("adminOpstine");
	if(forma){
		forma.page.value = p;
		forma.submit.click();
	}
}
function afterDeleteUllok(response){
	var fform = document.getElementById('ullokFilterForm');
	var sbmt = fform.getElementsByTagName("input");
	sbmt = sbmt[sbmt.length-1];
	alert(response);
	sbmt.click();
}